<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>performancetable.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_performancetable.js.html">performancetable.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A table for showing the results of performance testing.
<a name="line17"></a> *
<a name="line18"></a> * {@see goog.testing.benchmark} for an easy way to use this functionality.
<a name="line19"></a> *
<a name="line20"></a> * @author attila@google.com (Attila Bodis)
<a name="line21"></a> * @author nicksantos@google.com (Nick Santos)
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.testing.PerformanceTable&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.dom&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.PerformanceTimer&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * A UI widget that runs performance tests and displays the results.
<a name="line33"></a> * @param {Element} root The element where the table should be attached.
<a name="line34"></a> * @param {goog.testing.PerformanceTimer=} opt_timer A timer to use for
<a name="line35"></a> *     executing functions and profiling them.
<a name="line36"></a> * @param {number=} opt_precision Number of digits of precision to include in
<a name="line37"></a> *     results.  Defaults to 0.
<a name="line38"></a> * @constructor
<a name="line39"></a> */
<a name="line40"></a>goog.testing.PerformanceTable = function(root, opt_timer, opt_precision) {
<a name="line41"></a>  /**
<a name="line42"></a>   * Where the table should be attached.
<a name="line43"></a>   * @type {Element}
<a name="line44"></a>   * @private
<a name="line45"></a>   */
<a name="line46"></a>  this.root_ = root;
<a name="line47"></a>
<a name="line48"></a>  /**
<a name="line49"></a>   * Number of digits of precision to include in results.
<a name="line50"></a>   * Defaults to 0.
<a name="line51"></a>   * @type {number}
<a name="line52"></a>   * @private
<a name="line53"></a>   */
<a name="line54"></a>  this.precision_ = opt_precision || 0;
<a name="line55"></a>
<a name="line56"></a>  var timer = opt_timer;
<a name="line57"></a>  if (!timer) {
<a name="line58"></a>    timer = new goog.testing.PerformanceTimer();
<a name="line59"></a>    timer.setNumSamples(5);
<a name="line60"></a>    timer.setDiscardOutliers(true);
<a name="line61"></a>  }
<a name="line62"></a>
<a name="line63"></a>  /**
<a name="line64"></a>   * A timer for running the tests.
<a name="line65"></a>   * @type {goog.testing.PerformanceTimer}
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  this.timer_ = timer;
<a name="line69"></a>
<a name="line70"></a>  this.initRoot_();
<a name="line71"></a>};
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * @return {goog.testing.PerformanceTimer} The timer being used.
<a name="line76"></a> */
<a name="line77"></a>goog.testing.PerformanceTable.prototype.getTimer = function() {
<a name="line78"></a>  return this.timer_;
<a name="line79"></a>};
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Render the initial table.
<a name="line84"></a> * @private
<a name="line85"></a> */
<a name="line86"></a>goog.testing.PerformanceTable.prototype.initRoot_ = function() {
<a name="line87"></a>  this.root_.innerHTML =
<a name="line88"></a>      &#39;&lt;table class=&quot;test-results&quot; cellspacing=&quot;1&quot;&gt;&#39; +
<a name="line89"></a>      &#39;  &lt;thead&gt;&#39; +
<a name="line90"></a>      &#39;    &lt;tr&gt;&#39; +
<a name="line91"></a>      &#39;      &lt;th rowspan=&quot;2&quot;&gt;Test Description&lt;/th&gt;&#39; +
<a name="line92"></a>      &#39;      &lt;th rowspan=&quot;2&quot;&gt;Runs&lt;/th&gt;&#39; +
<a name="line93"></a>      &#39;      &lt;th colspan=&quot;4&quot;&gt;Results (ms)&lt;/th&gt;&#39; +
<a name="line94"></a>      &#39;    &lt;/tr&gt;&#39; +
<a name="line95"></a>      &#39;    &lt;tr&gt;&#39; +
<a name="line96"></a>      &#39;      &lt;th&gt;Average&lt;/th&gt;&#39; +
<a name="line97"></a>      &#39;      &lt;th&gt;Std Dev&lt;/th&gt;&#39; +
<a name="line98"></a>      &#39;      &lt;th&gt;Minimum&lt;/th&gt;&#39; +
<a name="line99"></a>      &#39;      &lt;th&gt;Maximum&lt;/th&gt;&#39; +
<a name="line100"></a>      &#39;    &lt;/tr&gt;&#39; +
<a name="line101"></a>      &#39;  &lt;/thead&gt;&#39; +
<a name="line102"></a>      &#39;  &lt;tbody&gt;&#39; +
<a name="line103"></a>      &#39;  &lt;/tbody&gt;&#39; +
<a name="line104"></a>      &#39;&lt;/table&gt;&#39;;
<a name="line105"></a>};
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * @return {Element} The body of the table.
<a name="line110"></a> * @private
<a name="line111"></a> */
<a name="line112"></a>goog.testing.PerformanceTable.prototype.getTableBody_ = function() {
<a name="line113"></a>  return this.root_.getElementsByTagName(goog.dom.TagName.TBODY)[0];
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Round to the specified precision.
<a name="line119"></a> * @param {number} num The number to round.
<a name="line120"></a> * @return {string} The rounded number, as a string.
<a name="line121"></a> * @private
<a name="line122"></a> */
<a name="line123"></a>goog.testing.PerformanceTable.prototype.round_ = function(num) {
<a name="line124"></a>  var factor = Math.pow(10, this.precision_);
<a name="line125"></a>  return String(Math.round(num * factor) / factor);
<a name="line126"></a>};
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Run the given function with the performance timer, and show the results.
<a name="line131"></a> * @param {Function} fn The function to run.
<a name="line132"></a> * @param {string=} opt_desc A description to associate with this run.
<a name="line133"></a> */
<a name="line134"></a>goog.testing.PerformanceTable.prototype.run = function(fn, opt_desc) {
<a name="line135"></a>  this.runTask(
<a name="line136"></a>      new goog.testing.PerformanceTimer.Task(/** @type {function()} */ (fn)),
<a name="line137"></a>      opt_desc);
<a name="line138"></a>};
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Run the given task with the performance timer, and show the results.
<a name="line143"></a> * @param {goog.testing.PerformanceTimer.Task} task The performance timer task
<a name="line144"></a> *     to run.
<a name="line145"></a> * @param {string=} opt_desc A description to associate with this run.
<a name="line146"></a> */
<a name="line147"></a>goog.testing.PerformanceTable.prototype.runTask = function(task, opt_desc) {
<a name="line148"></a>  var results = this.timer_.runTask(task);
<a name="line149"></a>  this.recordResults(results, opt_desc);
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Record a performance timer results object to the performance table. See
<a name="line155"></a> * {@code goog.testing.PerformanceTimer} for details of the format of this
<a name="line156"></a> * object.
<a name="line157"></a> * @param {Object} results The performance timer results object.
<a name="line158"></a> * @param {string=} opt_desc A description to associate with these results.
<a name="line159"></a> */
<a name="line160"></a>goog.testing.PerformanceTable.prototype.recordResults = function(
<a name="line161"></a>    results, opt_desc) {
<a name="line162"></a>  var average = results[&#39;average&#39;];
<a name="line163"></a>  var standardDeviation = results[&#39;standardDeviation&#39;];
<a name="line164"></a>  var isSuspicious = average &lt; 0 || standardDeviation &gt; average * .5;
<a name="line165"></a>  var resultsRow = goog.dom.createDom(&#39;tr&#39;, null,
<a name="line166"></a>      goog.dom.createDom(&#39;td&#39;, &#39;test-description&#39;,
<a name="line167"></a>          opt_desc || &#39;No description&#39;),
<a name="line168"></a>      goog.dom.createDom(&#39;td&#39;, &#39;test-count&#39;, String(results[&#39;count&#39;])),
<a name="line169"></a>      goog.dom.createDom(&#39;td&#39;, &#39;test-average&#39;, this.round_(average)),
<a name="line170"></a>      goog.dom.createDom(&#39;td&#39;, &#39;test-standard-deviation&#39;,
<a name="line171"></a>          this.round_(standardDeviation)),
<a name="line172"></a>      goog.dom.createDom(&#39;td&#39;, &#39;test-minimum&#39;, String(results[&#39;minimum&#39;])),
<a name="line173"></a>      goog.dom.createDom(&#39;td&#39;, &#39;test-maximum&#39;, String(results[&#39;maximum&#39;])));
<a name="line174"></a>  if (isSuspicious) {
<a name="line175"></a>    resultsRow.className = &#39;test-suspicious&#39;;
<a name="line176"></a>  }
<a name="line177"></a>  this.getTableBody_().appendChild(resultsRow);
<a name="line178"></a>};
<a name="line179"></a>
<a name="line180"></a>
<a name="line181"></a>/**
<a name="line182"></a> * Report an error in the table.
<a name="line183"></a> * @param {*} reason The reason for the error.
<a name="line184"></a> */
<a name="line185"></a>goog.testing.PerformanceTable.prototype.reportError = function(reason) {
<a name="line186"></a>  this.getTableBody_().appendChild(
<a name="line187"></a>      goog.dom.createDom(&#39;tr&#39;, null,
<a name="line188"></a>          goog.dom.createDom(&#39;td&#39;, {&#39;class&#39;: &#39;test-error&#39;, &#39;colSpan&#39;: 5},
<a name="line189"></a>              String(reason))));
<a name="line190"></a>};
</pre>


</body>
</html>
